package com.fyh.controller;

import com.fyh.constant.MessageConstant;
import com.fyh.entity.chat.SimpleContact;
import com.fyh.entity.common.Result;
import com.fyh.entity.community.PostVO;
import com.fyh.entity.community.ReplyVO;
import com.fyh.entity.job.DeliveryRecord;
import com.fyh.entity.user.SimpleCandidate;
import com.fyh.entity.user.User;
import com.fyh.entity.user.UserLogin;
import com.fyh.entity.user.Vitae;
import com.fyh.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RequestMapping("/user")
@RestController
@Slf4j
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result<UserLogin> login(@RequestBody UserLogin user) {

        log.info("用户登录：{}", user);

        if (user == null) {
            return Result.error("登录信息不能为空");
        }

        UserLogin userLogin = userService.login(user);
        if(userLogin != null) {
            return Result.success(userLogin);
        }
        return Result.error(MessageConstant.USERNAME_PASSWORD_ERROR);
    }

    /**
     * 用户登出
     * @param userId
     * @return
     */
    @PostMapping("/logout")
    public Result loginout(@RequestHeader("userId") Long userId) {
        log.info("用户登出：{}", userId);
        userService.logout(userId);
        return Result.success();
    }


    /**
     * 注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        log.info("用户注册：{}", user);
        if (user == null) {
            return Result.error("用户信息不能为空");
        }

        String message = userService.register(user);
        if(message == MessageConstant.USERNAME_IS_EXIST){
            return Result.error(message);
        }
        return Result.success();
    }

    /**
     * 首页
     */
    @GetMapping("/home")
    public Result<Map<String, List<String>>> home(){
        log.info("首页");
        Map<String, List<String>> tagList = userService.getTagList();
        if(tagList == null) {
            return Result.error(MessageConstant.GET_TAG_LIST_FAILED);
        }
        return Result.success(tagList);
    }

    /**
     * 通过id获取用户信息
     */
    @GetMapping("/getUserInfo")
    public Result<User> getUserInfo(HttpServletRequest request) {
        // 从请求头中获取userId
        long id = Long.parseLong(request.getHeader("userId"));
        log.info("获取用户信息：{}", id);
        User user = userService.getUserInfo(id);
        return Result.success(user);
    }

    /**
     * 通过id获取用户名称
     */
    @GetMapping("/getNickname")
    public Result<String> getNickname(@RequestParam("userId") Long id) {
        log.info("获取用户名称：{}", id);
        String name = userService.getNickname(id);
        return Result.success(name);
    }

    /**
     * 上传头像文件
     */
    @PostMapping("/uploadAvatar")
    public Result<String> uploadAvatar(@RequestParam("file") MultipartFile file) {
        log.info("上传头像文件：{}", file);
        String filePath = userService.uploadAvatar(file);
        if(filePath == null){
            return Result.error(MessageConstant.UPLOAD_FILE_FAILED);
        }
        return Result.success(filePath);
    }

    /**
     * 更新用户信息
     */
    @PostMapping("/updateUserInfo")
    public Result updateUserInfo(@RequestBody User user) {
        log.info("更新用户信息：{}", user);
        userService.update(user);
        return Result.success();
    }

    /**
     * 获取用户个人简历
     */
    @GetMapping("/getUserVitae")
    public Result<Vitae> getUserVitae(@RequestParam("userId") Long userId) {
        log.info("获取用户简历：{}", userId);
        Vitae userVitae = userService.getUserVitae(userId);
        return Result.success(userVitae);
    }

    /**
     * 获取求职者基础信息列表
     */
    @PostMapping("/getSimpleCandidateList")
    public Result<List<SimpleCandidate>> getSimpleCandidateList(@RequestBody List<DeliveryRecord> deliveryRecordList){
        List<SimpleCandidate> simpleCandidateList = userService.getSimpleCandidateList(deliveryRecordList);
        return Result.success(simpleCandidateList);
    }

    /**
     * 获取求职者基础信息
     */
    @GetMapping("/getSimpleCandidate")
    public Result<SimpleCandidate> getSimpleCandidate(@RequestParam("userId") Long userId) {
        SimpleCandidate simpleCandidate = userService.getSimpleCandidate(userId);
        return Result.success(simpleCandidate);
    }

    /**
     * 获取联系人头像链接和用户在线状态
     */
    @PostMapping("/getContactListAvatarPath")
    public Result<List<SimpleContact>> getContactListAvatarPath(@RequestBody List<SimpleContact> simpleContactList){
        simpleContactList = userService.getContactListAvatarPath(simpleContactList);
        return Result.success(simpleContactList);
    }

    /**
     * 补全用户昵称和用户头像文件路径（用于帖子列表）
     *
     * @param postVOList
     * @return
     */
    @PostMapping("/selectNicknameAndAvaterPathForPost")
    public Result<List<PostVO>> selectNicknameAndAvaterPathForPost(@RequestBody List<PostVO> postVOList){
        log.info("补全用户昵称和用户头像文件路径");
        postVOList = userService.selectNicknameAndAvaterPathForPost(postVOList);
        return Result.success(postVOList);
    }
    /**
     * 补全用户昵称和用户头像文件路径（用于回复列表）
     *
     * @param replyVOList
     * @return
     */
    @PostMapping("/selectNicknameAndAvaterPathForReply")
    public Result<List<ReplyVO>> selectNicknameAndAvaterPathForReply(@RequestBody List<ReplyVO> replyVOList){
        log.info("补全用户昵称和用户头像文件路径");
        replyVOList = userService.selectNicknameAndAvaterPathForReply(replyVOList);
        return Result.success(replyVOList);
    }
}
